package com.shopping.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.shopping.bean.OrderGoods;
import com.shopping.bean.OrderType;
import com.shopping.dao.OrderGoodsDao;
import com.shopping.util.DBUtil;

public class OrderGoodsDaoImpl implements OrderGoodsDao {

	JdbcTemplate jt = new JdbcTemplate(DBUtil.getDataSource());
	@Override
	public List<OrderGoods> selectAll(Map<String, String[]> condition) {
		String sql = "select * from orderGoods where 1=1 ";
		int pageNum = 1;
		int pageSize = 5;
		
		List list = new ArrayList();
		for(String key:condition.keySet()) {
			if("keyword".equals(key) && !"".equals(condition.get(key)[0])) {
				sql += "and time LIKE ? ";
				list.add("%"+condition.get(key)[0]+"%");
			}
			
			if("keyword".equals(key) && !"".equals(condition.get(key)[0])) {
				sql += "or userId = ? ";
				list.add("%"+condition.get(key)[0]+"%");
			}
			
		}
		try {
			pageNum = Integer.parseInt(condition.get("pageNum")[0]);
		}catch(Exception e) {
			
		}
		try {
			pageSize = Integer.parseInt(condition.get("pageSize")[0]);
		}catch(Exception e) {
			
		}
		sql += "LIMIT ?,?";
		list.add((pageNum-1)*pageSize);
		list.add(pageSize);
		
		List<OrderGoods> goods = jt.query(sql, new BeanPropertyRowMapper<OrderGoods>(OrderGoods.class),list.toArray());
		return goods;
	}
	@Override
	public int selectTotalRows(Map<String, String[]> condition) {
		String sql ="SELECT count(1) FROM orderGoods WHERE 1 = 1 ";
		List params = new ArrayList(); //由于sql语句是动态的 ?的个数 不确定，所以没拼接一个sql 我们就先把这个参数放在 list集合当中
		for(String key: condition.keySet()) {
			if("keyword".equals(key) && !"".equals(condition.get(key)[0])) {
				sql += "or time like ? ";
				params.add(condition.get(key)[0]);
			}
			if("keyword".equals(key) && !"".equals(condition.get(key)[0])) {
				sql += "or userId = ? ";
				params.add("%"+condition.get(key)[0]+"%");
			}
			
		}
		
			return jt.queryForObject(sql, Integer.class,params.toArray());
	}
	@Override
	public int insertGoogsOnCar(Map<String,String[]> condition) {//添加订单
		String sql = "insert into ordertype values(default,?,?,0,0,'正在出货','未收货')";
		String sql1 = "insert into orderGoods(priceSum,userId,time) values(?,?,?)";
		int result = 0;
		int num = 0;
		Date date = new Date(System.currentTimeMillis());
		Set<Entry<String,String[]>> set = condition.entrySet();
		for(Entry<String,String[]> s:set) {
			System.out.println(condition.get(s.getKey()).length);
			for(int i = 0; i <s.getValue().length;i++) {
				String[] id = condition.get("userId");
				String[] price = condition.get("sumPrice");
				if(s.getKey().equals("userId") || s.getKey().equals("sumPrice")) {
					if(num ==0) {
						num = jt.update(sql1,price[0],id[0],date);
					}
					return 1;
				}
				
				int ids = Integer.parseInt(id[0]);
				
				result = jt.update(sql,s.getValue()[i],ids);
				
				if(result == 0) {
					return 0;
				}
			}
		}
		return result;
	}

	@Override
	public List<OrderType> selectOrderByUserId(int userId) {
		String sql = "select * from orderType where userId = ?";
		return jt.query(sql, new BeanPropertyRowMapper<OrderType>(OrderType.class),userId);
	}

}
